//
//  SYS_RemeberPwdVC.m
//  System_App
//
//  Created by 魏配军 on 2017/11/28.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_RemeberPwdVC.h"
#import "SYS_RemeberPwdCell.h"
#import "SYS_TimeButton.h"

@interface SYS_RemeberPwdVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    SYS_TimeButton *timeButton;
}
@property (nonatomic, strong) DSNewTableView *mainTableView;
@property (nonatomic,strong) UIButton *confirmButton;

@end

@implementation SYS_RemeberPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationBar];
    [self initWithUI];
}

#pragma mark -- setter and getter
- (void)setNavigationBar{
    self.title = @"找回密码";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"SYS_Public_back" highImage:nil title:nil target:self action:@selector(backToPrevious)];
    self.view.backgroundColor = DGHColor(242, 242, 242);
}

#pragma mark   -------  UI  ---------
-(void) initWithUI
{
    _mainTableView=[[DSNewTableView alloc] initWithFrame:CGRectMake(0,0, SCREENWIDTH, 160) style:UITableViewStylePlain];
    _mainTableView.scrollEnabled=NO;
    _mainTableView.delegate=self;
    _mainTableView.dataSource=self;
    _mainTableView.backgroundColor=DGHColor(242, 242, 242);
    _mainTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];
    
    //添加确认修改按钮
    _confirmButton=[[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_mainTableView.frame)+20, SCREENWIDTH-40, 40)];
    [_confirmButton setBackgroundColor:MainColor];
    [_confirmButton setTitle:@"确认修改" forState:UIControlStateNormal];
    _confirmButton.titleLabel.textAlignment=NSTextAlignmentCenter;
    _confirmButton.titleLabel.font=[UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [_confirmButton setTitleColor:DGHColor(255, 255, 255) forState:UIControlStateNormal];
    _confirmButton.layer.masksToBounds=YES;
    _confirmButton.layer.cornerRadius=5;
    [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_confirmButton];
}

#pragma mark  ----------获取验证码---------------
-(void) timeButtonClick
{
    UITextField *textFiled=[self.view viewWithTag:1000];
    [self.view endEditing:YES];
    if (textFiled.text.length==0) {
        [MBProgressHUD showSuccess:@"手机号不能为空" toView:self.view];
        return;
    }
    if (![SYS_HelpModel isValidateMobile:textFiled.text]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
    NSDictionary *parma=@{@"phone":textFiled.text};
    [SYS_WPJ_RequestHttpTool remeberPwdToGetEmsCode:parma success:^(id returnValue) {
        if ([returnValue[@"flag"] isEqualToString:@"true"]) {
            
            [MBProgressHUD showSuccess:@"正在获取验证码" toView:self.view];
            //设定定时器为60秒
            timeButton.durationOfCountDown=60;

        }else{
            [MBProgressHUD showSuccess:returnValue[@"exp"] toView:self.view];
        }
    } failure:^{
        RequestFailure;
    }];
}

#pragma mark  ----------确认修改---------------
-(void) confirmButtonClick
{
    UITextField *textFiled=[self.view viewWithTag:1000];
    UITextField *textFiled1=[self.view viewWithTag:1001];
    UITextField *textFiled2=[self.view viewWithTag:1002];
    UITextField *textFiled3=[self.view viewWithTag:1003];

    [self.view endEditing:YES];
    if (textFiled.text.length==0) {
        [MBProgressHUD showSuccess:@"手机号不能为空" toView:self.view];
        return;
    }
    if (![SYS_HelpModel isValidateMobile:textFiled.text]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
    if (textFiled1.text.length==0) {
        [MBProgressHUD showSuccess:@"验证码不能为空" toView:self.view];
        return;
    }
    if (textFiled2.text.length==0) {
        [MBProgressHUD showSuccess:@"密码不能为空" toView:self.view];
        return;
    }
    if (textFiled2.text.length<6) {
        [MBProgressHUD showSuccess:@"密码不能少于六位" toView:self.view];
        return;
    }
    if (textFiled3.text.length==0) {
        [MBProgressHUD showSuccess:@"确认密码不能为空" toView:self.view];
        return;
    }
    if (![textFiled2.text isEqualToString:textFiled3.text]) {
        [MBProgressHUD showSuccess:@"两次密码输入不一致" toView:self.view];
        return;
    }
    NSDictionary *parma=@{@"phone":textFiled.text,@"newPassword":textFiled2.text,@"smsCode":textFiled1.text};
    [self showHudInView:self.view hint:@"正在加载..."];
    [SYS_WPJ_RequestHttpTool remeberPwdToFindPwd:parma success:^(id returnValue) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([returnValue[@"flag"] isEqualToString:@"true"]) {
            [MBProgressHUD showSuccess:@"找回密码成功" toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showSuccess:returnValue[@"exp"] toView:self.view];
        }
    } failure:^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        RequestFailure;
    }];
}

#pragma mark   ---------UITableViewDelegate,UITableViewDataSource-------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYS_RemeberPwdCell *cell = [[SYS_RemeberPwdCell alloc]initWithTableView:tableView reuseId:@"SYS_RemeberPwdCell"];
    cell.inputTextField.delegate=self;
    cell.inputTextField.tag=1000+indexPath.row;
    if (indexPath.row==0) {
        cell.iconImageView.image=[UIImage imageNamed:@"SYS_newRemeber_sj"];
        cell.nameLabel.text=@"+86";
        cell.inputTextField.placeholder=@"请输入手机号";
        cell.inputTextField.keyboardType=UIKeyboardTypeNumberPad;
        cell.isPhoneText=YES;
        timeButton=[[SYS_TimeButton alloc]initWithFrame:CGRectMake(SCREENWIDTH-70, 5, 65, 30)];
        [timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [timeButton setTitleColor:DSCOLOR_WHITE forState:0];
        [timeButton setBackgroundColor:MainColor];
        timeButton.layer.masksToBounds=YES;
        timeButton.layer.cornerRadius=3;
        timeButton.titleLabel.font=[UIFont systemFontOfSize:10];
        [timeButton addTarget:self action:@selector(timeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:timeButton];
        
    }else if (indexPath.row==1){
        cell.iconImageView.image=[UIImage imageNamed:@"SYS_newRemeber_code"];
        cell.nameLabel.text=@"验证码";
        cell.inputTextField.placeholder=@"请输入您收到的短信验证码";
        cell.inputTextField.keyboardType=UIKeyboardTypeNumberPad;
    }else if (indexPath.row==2){
        cell.iconImageView.image=[UIImage imageNamed:@"SYS_newRemeber_mm"];
        cell.nameLabel.text=@"密码";
        cell.inputTextField.placeholder=@"请输入6~20位字符密码";
        cell.inputTextField.secureTextEntry=YES;
    }else if (indexPath.row==3){
        cell.iconImageView.image=[UIImage imageNamed:@"SYS_newRemeber_mm"];
        cell.nameLabel.text=@"密码确认";
        cell.inputTextField.placeholder=@"请再次输入密码";
        cell.inputTextField.secureTextEntry=YES;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

#pragma mark    --------UITextFieldDelegate---------
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

{  //string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (textField.tag == 1000)  //判断是否时我们想要限定的那个输入框
        
    {
        if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警告
            
            textField.text = [toBeString substringToIndex:20];
            
            return NO;
        }
    }else if (textField.tag==1002||textField.tag==1003){
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789._qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        
        if ([toBeString length] > 20) { //如果输入框内容大于11则弹出警告

            
            textField.text = [toBeString substringToIndex:20];
            
            return NO;
            
        }else{
            return [string isEqualToString:filtered];
        }
    }
    return YES;
}

- (void)backToPrevious
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
